<?php
declare (strict_types=1);

namespace app\service;

use app\controller\admin\GoodsMatchParam;
use app\controller\admin\UserMatchParam;
use app\model\MatchParam;
use app\model\SubCategoryMatchGroup;
use app\model\TUserMatchParam;
use app\traits\ServiceTrait;
use think\facade\Db;

/**
 * MatchParamService
 * Class MatchParamService
 * @package app\service
 */
class MatchParamService
{
    //模型，带命名空间
    public static $model = 'app\model\MatchParam';
    //模型主键
    public static $pk = 'id';
    //name字段名称
    public static $name = 'group_name';

    use ServiceTrait;

    /**
     * @param $subCategoryId
     * @param $labelId
     * @return array
     */
    public static function getMatchBySubCategoryIdAndLabId($subCategoryId, $labelId)
    {
        // 列表
        $table_match_param = MatchParam::$_table;
        $table_sub_category_match_group = SubCategoryMatchGroup::$_table;
        $table_user_match_param = TUserMatchParam::$_table;
        // 先根据$subCategoryId查找 SubCategoryMatchGroup match_group_id
        $sub_category_match_group_ids = Db::table($table_sub_category_match_group)->where(['sub_category_id' => $subCategoryId])->column('match_group_id');
        if (empty($sub_category_match_group_ids)) {
            return [];
        }
        // 根据match_group_id 查找 TUserMatchParam 的数据
        $match_param_list = Db::table($table_match_param)->where([['group_id', 'in', $sub_category_match_group_ids]])->select();
        if (empty($match_param_list)) {
            return [];
        }
        $user_match_param_list = Db::table($table_user_match_param)->where([['label_id', '=', $labelId], ['sub_category_id', '=', $subCategoryId]])->select();
        $user_match_param_data = [];
        if (!empty($user_match_param_list)) {
            foreach ($user_match_param_list as $user_match_param) {
                $user_match_param_data[$user_match_param['match_group_id']] = $user_match_param['param_val'];
            }
        }

        $tixing = config('goods.labelsTixing');
        $tixingIds = $tixing['ids'];
        $tixingGroupIds = $tixing['groupIds'];
        $groupIds = [];
        if (in_array($labelId, $tixingIds)) {
            $groupIds = $tixingGroupIds;
        }
        $cates = config('goods.labelsCate');
        $labelIds = array_keys($cates);
        $groupIdsShow = [];
        $spec = $cates[$labelId]['spec'] ?? [];
        if (in_array($labelId, $labelIds)) {
            $groupIdsShow = $cates[$labelId]['groupIds'];
        }
        if (!empty($spec)) {
            $groupIdsShowSpec = $cates[$labelId]['spec'][$subCategoryId] ?? [];
        }
        if (!empty($groupIdsShowSpec)) {
            $groupIdsShow = $groupIdsShowSpec;
        }
        $type = $cates[$labelId]['type'] ?? 'except';
        $groupData = [];
        foreach ($match_param_list as $v) {
            if (!empty($groupIds)) {
                if (in_array($v['group_id'], $groupIds)) continue;
            }
            if (!empty($groupIdsShow)) {
                if ($type == 'except') {
                    // 除外
                    if (in_array($v['group_id'], $groupIdsShow)) continue;
                } else {
                    if (!in_array($v['group_id'], $groupIdsShow)) continue;
                }
            }
            $groupData[$v['group_id']]['id'] = $v['id'];
            $groupData[$v['group_id']]['groupId'] = $v['group_id'];
            $groupData[$v['group_id']]['labels'] = '';
            $groupData[$v['group_id']]['name'] = $v['group_name'];
            $groupData[$v['group_id']]['params'][] = [
                'createTime' => '',
                'groupId' => $v['group_id'],
                'groupName' => $v['group_name'],
                'id' => $v['id'],
                'name' => $v['name'],
                'remark' => '',
                'sortNo' => $v['sort_no'],
                'type' => $v['type'],
                'updateTime' => '',
            ];
        }
        foreach ($groupData as &$match) {
            $match['checkValue'] = $user_match_param_data[$match['groupId']] ?? '';
        }
        return array_values($groupData);
    }

    /**
     * @param $subCategoryId
     * @return array
     */
    public static function getMatchBySubCategory($subCategoryId, $goodsId = false)
    {
        // 列表
        $table_match_param = MatchParam::$_table;
        $table_user_match_param = TUserMatchParam::$_table;
        // 先根据$subCategoryId查找 UserMatchParam id
        $match_param_ids = Db::table($table_user_match_param)->where(['sub_category_id' => $subCategoryId])->column('match_group_id');
        if (empty($match_param_ids)) {
            return [];
        }
        // 根据match_group_id 查找 TUserMatchParam 的数据
        $match_param_list = Db::table($table_match_param)->where([['group_id', 'in', $match_param_ids]])->select();
        if (empty($match_param_list)) {
            return [];
        }

        $goodsMatch = [];
        if ($goodsId) {
            $goodsMatch = \app\model\GoodsMatchParam::where(['goods_id' => $goodsId])->field(['match_group_id', 'param_val'])->select()->toArray();
        }
        $goodsMatchData = [];
        if (!empty($goodsMatch)) {
            foreach ($goodsMatch as $g) {
                $goodsMatchData[$g['match_group_id']] = $g['param_val'];
            }
        }

        $groupData = [];
        foreach ($match_param_list as $v) {
            if ($goodsId && !empty($goodsMatchData)) {
                $groupData[$v['group_id']]['checkValue'] = $goodsMatchData[$v['group_id']] ?? '';
            } else {
                $groupData[$v['group_id']]['checkValue'] = '';
            }
            $groupData[$v['group_id']]['id'] = $v['id'];
            $groupData[$v['group_id']]['labels'] = '';
            $groupData[$v['group_id']]['name'] = $v['group_name'];
            $groupData[$v['group_id']]['params'][] = [
                'createTime' => '',
                'groupId' => $v['group_id'],
                'groupName' => $v['group_name'],
                'id' => $v['id'],
                'name' => $v['name'],
                'remark' => '',
                'sortNo' => $v['sort_no'],
                'type' => $v['type'],
                'updateTime' => '',
            ];
        }
        return array_values($groupData);
    }
}
